package com.xqc.rabbit.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.xqc.rabbit.ConnectionUtils;

/**
* @author 作者 Gavin.Dai/SNSSZ00036
* @dateTime 创建时间：2020年8月11日 下午2:05:39
* @version V1.0.0
* 类说明
*/
public class TopicConsome2 {
	public static final String QUEUE_NAME = "topic_queue_2";
	
	public static void main(String[] args) throws IOException, TimeoutException {
		Connection conn = ConnectionUtils.getConn();
		Channel channel = conn.createChannel();
		//消费者声明自己的队列
		channel.queueDeclare(QUEUE_NAME, true, false, false, null);
		// 声明exchange，指定类型为direct
		channel.exchangeDeclare(TopicProduct.EXCHANGE_NAME, BuiltinExchangeType.TOPIC,true);
		//routingKey=fail
		String routingKey = "success.*";
		//消费者将队列与交换机进行绑定
		channel.queueBind(QUEUE_NAME, TopicProduct.EXCHANGE_NAME, routingKey);
		//handleDelivery父类没有实现，需要自己写
		DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println("消费者1："+new String(body, "UTF-8"));
			}
		};
		//声明消费者队列
		channel.basicConsume(QUEUE_NAME, true,defaultConsumer);
	}
}
